﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataPreparation;

namespace TMSimulator
{
    class CosineSimilarity : IDistance
    {
        /// <summary>
        /// return the new accumulative tmsocre of the user
        /// </summary>
        /// <param name="name"></param>
        /// <param name="user"></param>
        /// <param name="gradedDB"></param>
        /// <returns></returns>
        public double getTMScore(string name, User user, DataPreparation.GradedDB gradedDB)
        {
            double tempMax;
            double tmscoreToAdd;
            IItem temp = null;
          
            if (user.History == null)
                return 0;
            if (user.History.Count == 0) // if this the first doc of the user, the tmscore will be the tmscore of the doc,no need for the alghoritm
            {
                return gradedDB.items[name].getTMScore();
            }

            double currentTmScore = user.History.Values.Max(); // the current tmscore of the user
            foreach (FileAccess fname in user.History.Keys)    //if the doc already exist in the users' history
            {
                if (fname.FilePath.CompareTo(name)==0)
                    return currentTmScore;
            }

            // find the max similarity
            double max = -1;
            temp = gradedDB.items[name];

            foreach (FileAccess s in user.History.Keys)
            {
                if (temp.getName().CompareTo(s.FilePath) != 0)
                {
                    tempMax = gradedDB.GetSimilarity(gradedDB.items[s.FilePath], temp);
                    if (tempMax > max)
                        max = tempMax;
                }
            }

            tmscoreToAdd = (double)(1 - max) * temp.getTMScore(); // the tmscore calculatin- based on the previous tmscore of the user and the new one
            

            return currentTmScore + tmscoreToAdd;
        }
    }
}
